#include "CURLHttpConnection.h"
#include "ConnectionManager.h"

LIB_UTIL_NAMESPACE_BEGIN

ConnectionManager* ConnectionManager::GetConnectionManager()
{
	static CURLConnectionManager connectionManager;
	return &connectionManager;
}

bool ConnectionManager::GetHttpData(const char* url, 
									const int& port, 
									tDataBuffer* outData, 
									const tDataBuffer* inData,
									bool decrypt)
{
	if(outData == NULL)
		return false;

	ConnectionManager* connectionManager = GetConnectionManager();
	tConnectionData::Method type = tConnectionData::GET;
	if(inData != NULL)
		type = tConnectionData::POST;
	tConnectionData connectionData = {
		type, url, port, inData, outData, 0, false,
	};
	HttpConnection* connection = connectionManager->OpenConnection(connectionData);
	bool ret = connection->Connect();
	connection->Destroy();

	if(!decrypt || !ret)
		return ret;

	tDataBuffer tmp;
	EncryptHelper* encrypter = EncryptHelper::CreateEncryptHelper();
	if(!encrypter->DecryptData(*outData, tmp))
		return false;
	*outData = tmp;
	return true;
}

LIB_UTIL_NAMESPACE_END

